package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.common.PageResult;
import com.xrit.sunnyday.common.QueryPageBean;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderSettingService;
import com.xrit.sunnyday.common.MessageConstant;
import com.xrit.sunnyday.common.Result;
import com.xrit.sunnyday.common.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {
    /*
    远程调用
     */
    @Reference
    public OrderSettingService orderSettingService;
    /*
    文件上传
    使用poi工具类读取用户上传的Excel文件内容
    将其添加到数据库中
    使用MultipartFile springmvc中所有文件都用它接收
     */
    @RequestMapping("/upload.do")
    public Result upload(MultipartFile excelFile){
        try {
            //1,使用工具类读取Excel
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            //3,创建SimpleDateFormat
            SimpleDateFormat sdf = new SimpleDateFormat(POIUtils.DATE_FORMAT);
            //5,创建list<ordersetting>
            ArrayList<OrderSetting> list = new ArrayList<>();
            //2,遍历该Excel
            for (String[] strings : readExcel) {
                //3.1 将Excel中的string类型日期转为data类型日期
                Date date = sdf.parse(strings[0]);
                //3.2 将Excel中的string类型可预约人数转为int类型可预约人数
                int kyyNumber = Integer.valueOf(strings[1]);
                //4,将处理完的值放入ordersetting对象中
                OrderSetting orderSetting = new OrderSetting(date, kyyNumber);
                //6,将该对象添加到list中
                list.add(orderSetting);
            }
            //调用添加方法
            orderSettingService.add(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    /*
    根据用户选择的月份查询预约信息
     */
    @RequestMapping("/findByMethod.do")
    public Result findByMethod (String Method){
        List<Map<String,Integer>> orderSettingList=orderSettingService.findByMethod(Method);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderSettingList);
    }
    /*
    根据日期修改可预约人数
     */
    @RequestMapping("/updateNumberByDate.do")
    public Result updateNumberByDate(@RequestBody OrderSetting orderSetting){
        // 调用服务更新
        orderSettingService.updateNumberByDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
    @RequestMapping("/findAllPage.do")
    public Result findAllPage(@RequestBody QueryPageBean queryPageBean){
        PageResult orderSettingList=orderSettingService.findAllPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orderSettingList);
    }

}
